!function () {
    layui.config({base: '/layui/lay/modules/'}).use(['table', 'laydate', 'dateformat','upload', 'form','common','status'], function () {
        //定义
        var $ = layui.jquery,
            table = layui.table,
            form = layui.form,
            upload = layui.upload,
            dateformat = layui.dateformat,
            laydate = layui.laydate,
            common = layui.common,//自定义模块
            layuiSta = layui.status;//自定义状态控件

        //启动富文本编辑
        var ueditor ;
        //定义总变量
        var Recruit = {
            tableId: "recruitTable",    //表格id
            recruitType:{'':'全部'},              //父菜单节点
            condition: {                //查询条件
                name: "",
                place:"",
                startDate: "",
                endDate: ""
            },
            editorTool :null, //富文本内容
        }

        /**
         * 初始化列表列
         * **/
        Recruit.initColumn = function(){
            return [[
                /*{type: 'checkbox', fixed: 'left'},*/
                {field: 'id', title: 'ID', width: 80,hide:true},
                {field: 'name', title: '招聘职位', width: 160},
                {field: 'place', title: '工作地点', width: 260},
                {field: 'workYear' ,title :'工作年限',width:120,templet:'#workYearClo'},
                {field: 'count' ,title :'招聘人数',width:120},
                {field: 'status' ,title :'招聘显示',width:120,templet:'#statusClo'},
                {field: 'releaseTime' ,title :'发布时间',width: 165, templet: function (row) {
                        return dateformat.format(row.releaseTime, 'yyyy-MM-dd hh:mm:ss');
                    }},
                {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 280, align: 'center'}
            ]];
        }


        /**
         * 查询方法
         * **/
        Recruit.search = function(){
            var queryData = {};
            queryData["name"] = $("#name").val();
            queryData["place"] = $("#place").val();
            queryData["startDate"] = $("#startDate").val();
            queryData["endDate"] = $("#endDate").val();
            table.reload(Recruit.tableId, {where: queryData,page: {curr: 1}});
        }

        /**
         * 新增记录
         * **/
        Recruit.addRecord =function (){

            window.location.href="/recruit/add";
            /*//初始化表单
            $('#editForm')[0].reset();
            $('#editForm :input').val("");//重置所有的input
            layuiSta.initStaMulti("#statusDiv",'1',true);//构建状态
            laydate.render({
                elem: '#releaseTime',
                type: 'datetime'
            });

            layui.form.render();


            //清空富文本编辑框
            ueditor = UE.getEditor('ueditorText');
            //玄学加载
            setTimeout(function(){
                ueditor.setContent("");
            },666);
            $('#ueditorText #edui1_bottombar').hide();

            //打开弹出层
            var index = layer.open({
                type: 1, maxmin: true, title: '新增招聘信息', shade: 0.4, shadeClose: false,
                area: ['850px', '600px'], content: $('#editForm').show(), btn: ['保存', '取消'],
                yes: function () {
                    $('#saveRecord').click();
                }, btn2: function () {
                    layer.closeAll();
                }, end: function () {
                    $('#editForm').hide();
                }
            });
            layer.full(index);*/
        }

        /**
         * 修改记录
         * **/
        Recruit.editRecord =function (data){

            window.location.href="/recruit/update?id="+data.id;
            /*//设置富文本编辑框
            ueditor = UE.getEditor('ueditorText');
            laydate.render({
                elem: '#releaseTime',
                type: 'datetime'
            });
            //初始化数据
            $.ajax({
                async: false,
                url: '/recruit/findRecruit?id=' + data.id,
                success: function (result) {
                    if (result && result.code == 200) {
                        var data = result.data;
                        //初始化表单
                        form.render('radio').val('editForm', data);
                        layuiSta.initStaMulti("#statusDiv",data.status,true);//构建状态
                        //玄学加载
                        if( data.recruitText != undefined ){
                            setTimeout(function(){
                                ueditor.setContent(data.recruitText);
                            },666);
                        }
                        $('#ueditorText #edui1_bottombar').hide();
                        //打开弹出层
                        var index = layer.open({
                            type: 1, maxmin: true, title: '修改招聘', shade: 0.4, shadeClose: false,
                            area: ['850px', '600px'], content: $('#editForm').show(), btn: ['保存', '取消'],
                            yes: function () {
                                $('#saveRecord').click();
                            }, btn2: function () {
                                layer.closeAll();
                            }, end: function () {
                                $('#editForm').hide();
                            }
                        });
                        layer.full(index);
                    }else
                        layer.alert(result.msg || '请求失败了！', {icon: 5, title: '提示'});
                },
                error: function () {
                    layer.alert('服务器出错啦！', {icon: 2, title: '错误'});
                }
            });*/
        }

        /**
         * 删除记录
         * **/
        Recruit.deleteRecord =function (data){
            $.ajax({
                async: false,
                type: 'post',
                url: '/recruit/delete?id=' + data.id,
                success: function (result) {
                    if (result && result.code == 200) {
                        layer.alert('操作成功！', {icon: 1, title: '提示'});
                        Recruit.search();
                    }
                },
                error: function () {
                    layer.alert('服务器出错啦！', {icon: 2, title: '错误'});
                }
            });
        }

        //初始化渲染列表
        //日期
        //日期
        var startDate = laydate.render({
            elem: '#startDate',
            done: function(value, date){
                endDate.config.min = date;
                endDate.config.min.month = date.month -1;
            }
        });
        var endDate =  laydate.render({
            elem: '#endDate',
            done: function(value, date){
                if($.trim(value) == ''){
                    var curDate = new Date();
                    date = {'date': curDate.getDate(), 'month': curDate.getMonth()+1, 'year': curDate.getFullYear()};
                }
                startDate.config.max = date;
                startDate.config.max.month = date.month -1;
            }
        });

        table.render({
            elem:"#recruitTable"
            ,url:"/recruit/listPage"
            ,cellMinWidth:80
            ,request: {pageName: 'pageNum', limitName: 'pageSize'}
            ,page: true
            ,cols:Recruit.initColumn(),
            cellMinHeight:200

        });

        // 搜索按钮点击事件
        $('#btnSearch').click(function () {
            Recruit.search();
        });

        // 重置点击事件
        $('#resetSearch').click(function () {
            $('#queryTools input').val('');
            $('#queryTools select').val('');
            Recruit.search();
        });

        // 添加按钮点击事件
        $('#btnAdd').click(function () {
            Recruit.addRecord();
        });

        //监听工具条
        table.on('tool(' + Recruit.tableId + ')',function(obj){
            var data = obj.data;
            var layEvent = obj.event;
            if( layEvent ==='detail'){
                //查看
                return false;
            } else if( layEvent ==='del'){
                layer.confirm('确定要删除该招聘信息吗？', function (index) {
                    Recruit.deleteRecord(data);
                    return false;
                });
            } else if( layEvent ==='edit'){
                Recruit.editRecord(data);
                return false;
            }
        });

        //自定义验证规则
        form.verify({
            nameLen: [
                /^[\d\D]{0,32}$/
                ,'最多输入32个字'
            ],
            countLen: [
                /^[\d\D]{0,10}$/
                ,'最多输入10个字'
            ],
            count:[
                /^\d*$/
                ,'只能输入非负的整数'
            ],
            yearLen:[
                /^[\d\D]{0,16}$/
                ,'最多输入16个字'
            ],
            addrLen:[
                /^[\d\D]{0,128}$/
                ,'最多输入128个字'
            ],
        });

        //监听提交事件
        form.on('submit(editRecord)',function(data){

            //获取富文本编辑内容
            var recruitText = ueditor.getContent();
            data.field['recruitText']=recruitText;

            var postUrl = !data.field['id'] ? '/recruit/addRecruit' : '/recruit/editRecruit';

            $.ajax({
                async: false, type: 'post',
                traditional: true,
                data: JSON.stringify(data.field),
                contentType:"application/json;charset=UTF-8",
                processData:false,
                url: postUrl,
                success: function (data) {
                    if (data && data.code == 200) {
                        layer.closeAll();
                        layer.alert('操作成功！', {icon: 1, title: '提示'});
                        Recruit.search();
                    } else
                        layer.alert(data.msg || '操作失败了！', {icon: 5, title: '提示'});
                },
                error: function () {
                    layer.closeAll();
                    layer.alert('服务器出错啦！', {icon: 2, title: '错误'});
                }
            });
            return false;
        });

    });
}();